--
-- Author: shunguo.chen
-- Date: 2017-06-12 13:10:17
--
-- TabsBtn

local modeName = string.getModeName(...)
local ClassRef = class(modeName)

ClassRef._tabs = nil
ClassRef._callBack = nil
ClassRef._default = nil
ClassRef._binds = nil

ClassRef._pressedBtn = nil

--是否点击一选中的按钮依然响应事件
ClassRef._isAlwaysHander = nil
--选择改变时执行的方法
ClassRef._selectFunc = nil

function ClassRef:ctor(tabs, callBack, default, binds, selectFunc)
	self._tabs = tabs or {}
	self._callBack = callBack
	self._default = default or 1
	self._binds = binds

	self._isAlwaysHander = false
	self._selectFunc = selectFunc

	for index, btn in pairs(self._tabs) do
		self:setBtnSelected(btn, false)
	    self:addEvent(btn, index)
	end

    self:pressBtn(self._default, true)
end

function ClassRef:addEvent(btn, index)
	local desc = btn:getDescription()
	if desc == "Button" then
		btn:addClickEventListener(function()
			self:pressBtn(index, true)
		end)
	elseif desc == "CheckBox" then
		btn:addEventListener(function()
			self:pressBtn(index, true)
		end)
	else
		display.uiAddClick(btn, function()
			self:pressBtn(index, true)
		end)
	end
end

function ClassRef:addBtn(btn, index)
	self._tabs[index] = btn
	self:addEvent(btn, index)
end

function ClassRef:setAlwaysHander(b)
	self._isAlwaysHander = b
end

function ClassRef:setBtnSelected(btn, b)
	local desc = btn:getDescription()
	if desc == "Button" then
		btn:setBright(not b)
		-- 文本有两种状态
		local name1 = btn:getChildByName("name_1")
		local name2 = btn:getChildByName("name_2")
		if name1 and name2 then
			name1:setVisible(not b)
			name2:setVisible(b)
		end
	elseif desc == "CheckBox" then
		btn:setSelected(b)
	else
		-- 固定两种状态，mask或者select
		local mask = btn:getChildByName("mask")
		if mask then
			mask:setVisible(not b)
		end
		local select = btn:getChildByName("select")
		if select then
			select:setVisible(b)
		end
		local selected = btn:getChildByName("selected")
		if selected then
			selected:setVisible(b)
		end
	end
	executeFunc(self._selectFunc, btn, b)
end

function ClassRef:pressBtn(index, isCallBack)
	local tabBtn = self._tabs[index]
	if self._pressedBtn ~= tabBtn then
	    local preBtn = self._pressedBtn
	    self._pressedBtn = tabBtn
	    if preBtn then
	    	self:setBtnSelected(preBtn, false)
	    end
	    self:setBtnSelected(self._pressedBtn, true)

	    if nil ~= self._callBack and isCallBack then
	        self._callBack(index, self._pressedBtn)
	    end

	    if nil ~= self._binds then
	        -- bings中可能有相同的panel, 所以先隐藏，再显示
	        for key, panel in pairs(self._binds) do
	        	if key ~= index then
	            	panel:setVisible(false)
	            end
	        end
	        if self._binds[index] then
				self._binds[index]:setVisible(true)
			end
		end
	else
		self:setBtnSelected(self._pressedBtn, true)
		if self._isAlwaysHander then
			if nil ~= self._callBack and isCallBack then
			    self._callBack(index, self._pressedBtn)
			end
		end
	end
end

function ClassRef:setTouchEnabled(enable)
	for index, tabBtn in pairs(self._tabs) do
		tabBtn:setTouchEnabled(enable)
	end
end

function ClassRef:initSelectStatus()
	if nil ~= self._pressedBtn then
		self:setBtnSelected(self._pressedBtn, false)
	    self._pressedBtn = nil
	end
end

function ClassRef:setSelected(index, isCallBack)
	isCallBack = isCallBack or false
	self:pressBtn(index, isCallBack)
end

function ClassRef:getBtn(index)
	return self._tabs[index]
end

return ClassRef
